package com.xingqing.controller;

import com.xingqing.domain.vo.Result;
import com.xingqing.exception.BusinessException;
import com.xingqing.exception.FileUploadException;
import com.xingqing.exception.NotFoundException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/test/exception")
public class TestExceptionController {

    @GetMapping("/business")
    public Result<Void> testBusinessException() {
        throw new BusinessException("这是一个业务异常测试");
    }

    @GetMapping("/notfound/{id}")
    public Result<Void> testNotFoundException(@PathVariable Long id) {
        throw new NotFoundException("用户", id);
    }

    @GetMapping("/upload")
    public Result<Void> testFileUploadException() {
        throw new FileUploadException("文件上传失败测试");
    }

    @GetMapping("/nullpointer")
    public Result<Void> testNullPointerException() {
        String str = null;
        str.length(); // 故意触发空指针异常
        return Result.success();
    }

    @GetMapping("/illegal/{param}")
    public Result<Void> testIllegalArgumentException(@PathVariable String param) {
        if ("error".equals(param)) {
            throw new IllegalArgumentException("参数不能为error");
        }
        return Result.success();
    }

    @GetMapping("/runtime")
    public Result<Void> testRuntimeException() {
        throw new RuntimeException("运行时异常测试");
    }

    @GetMapping("/unknown")
    public Result<Void> testUnknownException() throws Exception {
        throw new Exception("未知异常测试");
    }
}